import 'dart:developer';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/constant/api.dart';
import 'package:guochaoapp/common/utils/http_client.dart';
import 'package:guochaoapp/controller/home/home_controller.dart';
import 'package:guochaoapp/model/benchmark_entity.dart';
import 'package:guochaoapp/model/productbrand_entity.dart';
import 'package:guochaoapp/services/user_service.dart';

class GCBenchMarkDetailController extends GetxController {
  UserService userService = Get.find();

  List<String> days = ["30", "90", "120"];
  var daysIndex = 0.obs;
  var currentBrandIndex = 0;

  /*价格涨跌*/
  var productbrandList = RxList<ProductbrandData>.empty();
  var brandSelectIndex = 0;
  var maxBenchmark = 1.0;
  var minBenchmark = 0.0;

  /*图表数据*/
  var benchmarkList = RxList<BenchmarkData>.empty();
  var flspotList = RxList<FlSpot>.empty();


  /*查询成品管的品牌列表*/
  requestProductBrand() {
    HttpClient.request<ProductbrandEntity>(
        url: ProductBrandApi.productBrand,
        params: {"dealerId": userService.dealerId},
        method: Method.GET,
        onSuccess: (_, model) async {
          productbrandList.assignAll(model.data);

          requestBenchmark(currentBrandIndex, days[0], "6");
        });
  }

  /*查询基准价格变更*/
  requestBenchmark(int index, String day, String size) {
    HttpClient.request<BenchmarkEntity>(
        url: BenchmarkApi.benchmark,
        params: {
          "dealerId": userService.dealerId,
          "brandId": productbrandList[index].brandId,
          "interval": day,
          "size": size
        },
        method: Method.GET,
        onSuccess: (_, model) async {
          List<FlSpot> list = [];

          double maxValue = 0;
          double minValue = 0;

          for(int i = 0; i < model.data.length; i++) {
            if(i == 0) {
              maxValue = model.data[i].currentBenchmark;
              minValue = model.data[i].currentBenchmark;
            }

            if(model.data[i].currentBenchmark > maxValue) {
              maxValue = model.data[i].currentBenchmark;
            }

            if(model.data[i].currentBenchmark < minValue) {
              minValue = model.data[i].currentBenchmark;
            }
          }

          maxBenchmark = maxValue;
          minBenchmark = minValue;

          for(int i = 0; i < model.data.length; i++) {
//            log(((model.data[i].currentBenchmark / maxValue) * 5).toString() + "__" + model.data[i].currentBenchmark.toString() + "__" + maxValue.toString());
            list.add(FlSpot(i.toDouble(), (model.data[i].currentBenchmark / maxValue) * 5));
          }

          flspotList.assignAll(list);
          benchmarkList.assignAll(model.data);
        });
  }

  benchmarkSelected() {
    showModalBottomSheet(
        context: Get.context,
        builder: (context) {
          return Container(
              height: productbrandList.length.toDouble() * 50 + 50 + 10,
              child: Column(
                children: [
                  Container(
                    height: 50,
                    child: Row(children: [
                      SizedBox(width: 20),
                      Text("选择品牌",
                          style: TextStyle(
                              fontSize: 20,
                              color: Colors.black,
                              fontWeight: FontWeight.bold))
                    ]),
                  ),
                  Container(height: 10, color: Colors.grey.withOpacity(0.2)),
                  Expanded(
                      child: ListView.builder(
                    itemCount: productbrandList.length,
                    itemBuilder: (context, index) {
                      return GestureDetector(
                        onTap: () {
                          currentBrandIndex = index;
                          requestBenchmark(currentBrandIndex, days[index],"0");
                          Navigator.of(context).pop();
                        },
                        child: Container(
                            height: 50,
                            child: Row(children: [
                              SizedBox(width: 20),
                              Text(productbrandList[index].brandName),
                            ])),
                      );
                    },
                  ))
                ],
              ));
        });
  }

  daySelected() {
    showModalBottomSheet(
        context: Get.context,
        builder: (context) {
          return Container(
              height: days.length.toDouble() * 50 + 50 + 10,
              child: Column(
                children: [
                  Container(
                    height: 50,
                    child: Row(children: [
                      SizedBox(width: 20),
                      Text("选择时间跨度",
                          style: TextStyle(
                              fontSize: 20,
                              color: Colors.black,
                              fontWeight: FontWeight.bold))
                    ]),
                  ),
                  Container(height: 10, color: Colors.grey.withOpacity(0.2)),
                  Expanded(
                      child: ListView.builder(
                    itemCount: days.length,
                    itemBuilder: (context, index) {
                      return GestureDetector(
                        onTap: () {
                          requestBenchmark(currentBrandIndex, days[index],"0");
                          Navigator.of(context).pop();
                        },
                        child: Container(
                            height: 50,
                            child: Row(children: [
                              SizedBox(width: 20),
                              Text(days[index]),
                            ])),
                      );
                    },
                  ))
                ],
              ));
        });
  }
}
